/**
 * @author qiuxj
 * @text 责任片区管理
 */
var area = {
    init: function(){
        // 定义DWR
        var dwr = {
            createOrderQueue: orderQueueDwr.createOrderQueue,
            updateOrderQueue: orderQueueDwr.updateOrderQueue,
            getOrderQueue: orderQueueDwr.getOrderQueue,
            getAreaKls: areaDwr.getAreaKls,
            getComboKls: comboDwr.getComboKls,
            getStaticValueKls: staticDwr.getStaticValueKls,
            pageOrderQueue: orderQueueDwr.pageOrderQueue
        };
        
        //定义片区下拉列表
        var areaCbx = new CQCIS.form.DWRComboBox({
            ds: {
            	params: {},
                call: dwr.getAreaKls
            },
            width: 120,
            emptyText: '请选择片区',
            resizable: true,
            listeners: {
                //'select': function(o){
                //    grid.filter.areaId = o.getValue();
                //    grid.reload();
                //}
            }
        });
        
        // 增加按钮
        var addBtn = new CQCIS.Button({
            text: '添加 (<u>A</u>)',
            iconCls: 'btn_insert',
            handler: function(){
            		orderWin.load();
            		orderWin.form.findField("areaId").setValue(areaCbx.getValue());
            }
        });
        
        // 修改按钮
        var editBtn = new CQCIS.Button({
            text: '修改 (<u>E</u>)',
            iconCls: 'btn_modify',
            handler: function(){
                if (grid.getSelectedRow()) {
                    orderWin.load(grid.getSelectedRow()['orderId']);
                } else {
                    CQCIS.msg("请选择要修改的工单！");
                }
            }
        });
        
        var orderStateAcbx = new CQCIS.form.ArrayComboBox({
            width: 80,
            ds: {
                data: [['-1', '处理中'], ['0', '受理成功'], ['1', '受理失败'], ['2', '业务撤单']],
                fields: ['key', 'label']
            },
            emptyText: '受理状态',
            typeAhead: true,
            editable: false,
            forceSelection: false,
            minChars: 1
        });
        
        var stateAcbx = new CQCIS.form.ArrayComboBox({
            width: 80,
            ds: {
                data: [['-2', '该工单失效'], ['-1', '生成工单失败'], ['0', '未处理'], ['1', '已完工'], ['2', '处理中']],
                fields: ['key', 'label']
            },
            emptyText: '工单状态',
            typeAhead: true,
            editable: false,
            forceSelection: false,
            minChars: 1
        });
        
        var TypeAcbx = new CQCIS.form.ArrayComboBox( {
			width :80,
			ds : {
				data : [ 
					[ '0', '全部' ],
					[ '1', '工单号' ], 
					[ '2', '产品号' ], 
					[ '3', '受理时间' ], 
					[ '4', '工单状态' ], 
					[ '5', '受理状态' ], 
					[ '6', '片区' ]
				],
				fields : [ 'key', 'label' ]
			},
			emptyText :'请选择查询方式',
			typeAhead :true,
			editable :false,
			forceSelection :false,
			minChars :1,
			listeners : {
				'select' : function(o) {
					if (o.getValue() === '0') {
						OrderText.hide();
						NbrText.hide();
						SearchBox.hide();
	            		TimeText.hide();
	            		StartDate.hide();
	            		UtilText.hide();
	            		EndDate.hide();
	            		stateText.hide();
	            		stateAcbx.hide();
	            		orderStateText.hide();
	            		orderStateAcbx.hide();
	            		areaText.hide();
	            		areaCbx.hide();
					}
					if (o.getValue() === '1') {
					//按工单号
						OrderText.show();
						NbrText.hide();
						SearchBox.show();
	            		TimeText.hide();
	            		StartDate.hide();
	            		UtilText.hide();
	            		EndDate.hide();
	            		stateText.hide();
	            		stateAcbx.hide();
	            		orderStateText.hide();
	            		orderStateAcbx.hide();
	            		areaText.hide();
	            		areaCbx.hide();
	            		
	            		SearchBox.reset();
					}
					if (o.getValue() === '2') {
					//按帐号
						OrderText.hide();
						NbrText.show();
						SearchBox.show();
	            		TimeText.hide();
	            		StartDate.hide();
	            		UtilText.hide();
	            		EndDate.hide();
	            		stateText.hide();
	            		stateAcbx.hide();
	            		orderStateText.hide();
	            		orderStateAcbx.hide();
	            		areaText.hide();
	            		areaCbx.hide();
	            		
	            		SearchBox.reset();
					}
					if (o.getValue() === '3') {
					//按受理时间
						OrderText.hide();
						NbrText.hide();
						SearchBox.hide();
	            		TimeText.show();
	            		StartDate.show();
	            		UtilText.show();
	            		EndDate.show();
	            		stateText.hide();
	            		stateAcbx.hide();
	            		orderStateText.hide();
	            		orderStateAcbx.hide();
	            		areaText.hide();
	            		areaCbx.hide();
					}
					if (o.getValue() === '4') {
					//按工单状态
						OrderText.hide();
						NbrText.hide();
						SearchBox.hide();
	            		TimeText.hide();
	            		StartDate.hide();
	            		UtilText.hide();
	            		EndDate.hide();
	            		stateText.show();
	            		stateAcbx.show();
	            		orderStateText.hide();
	            		orderStateAcbx.hide();
	            		areaText.hide();
	            		areaCbx.hide();
					}
					if (o.getValue() === '5') {
					//按受理状态
						OrderText.hide();
						NbrText.hide();
						SearchBox.hide();
	            		TimeText.hide();
	            		StartDate.hide();
	            		UtilText.hide();
	            		EndDate.hide();
	            		stateText.hide();
	            		stateAcbx.hide();
	            		orderStateText.show();
	            		orderStateAcbx.show();
	            		areaText.hide();
	            		areaCbx.hide();
					}
					if (o.getValue() === '6') {
					//按片区
						OrderText.hide();
						NbrText.hide();
						SearchBox.hide();
	            		TimeText.hide();
	            		StartDate.hide();
	            		UtilText.hide();
	            		EndDate.hide();
	            		stateText.hide();
	            		stateAcbx.hide();
	            		orderStateText.hide();
	            		orderStateAcbx.hide();
	            		areaText.show();
	            		areaCbx.show();
					}
				}
			}
		});
		
		var TypeText = new Ext.form.Label( {
			text :'请选择查询方式：'
		});
		var OrderText = new Ext.form.Label( {
			text :'工单号：'
		});
		var NbrText = new Ext.form.Label( {
			text :'产品号：'
		});
        var TimeText = new Ext.form.Label( {
			text :'受理时间：'
		});
		
		var stateText = new Ext.form.Label( {
			text :'工单状态：'
		});
		
		var orderStateText = new Ext.form.Label( {
			text :'受理状态：'
		});
		
		var UtilText = new Ext.form.Label( {
			text :'至'
		});
		
		var areaText = new Ext.form.Label( {
			text :'请选择片区：'
		});
		
		// 定义开始时间选择框
        var StartDate = new CQCIS.form.DateTimeField({
            width: 100,
            emptyText: '请选择开始时间',
            readOnly: true,
            editable: false
        });
        
        // 定义结束时间选择框
        var EndDate = new CQCIS.form.DateTimeField({
            width: 100,
            emptyText: '请选择结束时间',
            readOnly: true,
            editable: false
        });

        // 构建输入文本框
        var SearchBox = new CQCIS.form.SearchBox({
            width: 120,
            emptyText: '请输入关键字'
        });

        // 添加信息表单字段
        var orderFormMeta = [{
            id: 'orderId',
            desc: '工单号',
            field: 'orderId',
            xtype: 'hidden'
        }, {
            desc: 'CRM工单号',
            field: 'crmSeq'
        }, {
            field: 'orderType',
            desc: '工单类型',
            type: 'int',
            xtype: 'hidden',
            obj: {
            	value: 1
            }
        }, {
            field: 'custName',
            desc: '客户名称',
            obj: {
            	allowBlank: false
            }
        }, {
            field: 'cardType',
            desc: '证件类型',
            type: 'int',
            xtype: 'hidden',
            obj: {
            	value: 1
            }
        }, {
            field: 'cardCode',
            desc: '证件号码'
        }, {
            field: 'prodType',
            desc: '产品类型',
            type: 'int',
            xtype: 'rcombo',
            obj: {
                ds: {
                	params: {
        				state: 1,
        				staticTypeId: 10000
        			},
                    call: dwr.getStaticValueKls,
                    fields: ['key', 'label']
                },
                emptyText: '请选择套餐',
                allowBlank: false,
                listeners: {
                    'select': function(o){
                            orderWin.form.findField('comboId').store.proxy.params = {prodType: o.getValue()};
                            orderWin.form.findField('comboId').enable();
                            orderWin.form.findField('comboId').store.reload();
                    }
                }
            },
            width: 80
        }, {
            field: 'comboId',
            desc: '业务名称',
            type: 'int',
            xtype: 'rcombo',
            obj: {
            	typeAhead: true,
                editable: true,
                forceSelection: true,
                minChars: 1,
				resizable: true,
                ds: {
                	params: {
        				state: 1
        			},
                    call: dwr.getComboKls,
                    fields: ['key', 'label']
                },
                disabled: true,
                emptyText: '请选择套餐',
                allowBlank: false
            },
            width: 80
        }, {
            field: 'accNbr',
            desc: '产品号码'
        }, {
            field: 'areaId',
            desc: '所属片区',
            type: 'int',
            xtype: 'rcombo',
            obj: {
                ds: {
                	params: {},
                    call: dwr.getAreaKls,
                    fields: ['key', 'label']
                },
                emptyText: '请选择分公司',
                allowBlank: false
            },
            width: 80
        }, {
            field: 'relaPhone',
            desc: '联系电话'
        }, {
            field: 'mobilePhone',
            desc: '移动电话'
        }, {
            field: 'relaAddr',
            desc: '联系人'
        }, {
            field: 'orderState',
            desc: '受理状态',
            type: 'int',
            xtype: 'acombo',
            obj: {
            	ds: {
                    data: [[-1, '处理中'], [0, '受理成功'], [1, '受理失败'], [2, '业务撤单']],
                    fields: ['key', 'label']
                },
                value: 1
            }
        }, {
            field: 'state',
            desc: '工单状态',
            type: 'int',
            xtype: 'acombo',
            obj: {
            	ds: {
                    data: [[-2, '该工单失效'], [-1, '生成工单失败'],[0, '未处理'], [1, '已完工'], [2, '处理中']],
                    fields: ['key', 'label']
                },
                value: 1
            }
        }, {
            field: 'createStaff',
            desc: '创建人',
            type: 'int',
            xtype: 'hidden'
        }, {
            field: 'modifyStaff',
            desc: '修改人',
            type: 'int',
            xtype: 'hidden'
        }, {
            field: 'createTime',
            desc: '创建时间',
            type: 'date',
            xtype: 'hidden'
        }, {
            field: 'modifyTime',
            desc: '修改时间',
            type: 'date',
            xtype: 'hidden'
        }, {
            field: 'dealDesc',
            desc: '处理描述'
            //,
            //xtype: 'htmleditor'
        }];

        // 列表数据对象
        var orderPageMeta = [{
            id: 'orderId',
            desc: '工单号',
            field: 'orderId'
        }, {
            desc: 'CRM工单号',
            field: 'crmSeq'
        }, {
            desc: '工单类型',
            field: 'orderType',
            type: 'int',
            hidden: true
        }, {
            desc: '客户名',
            field: 'custName'
        }, {
            desc: '证件类型',
            field: 'cardType',
            hidden: true
        }, {
            desc: '证件号码',
            field: 'cardCode',
            hidden: true
        }, {
        	desc: '产品类型',
            field: 'prodTypeDesc'
        }, {
        	desc: '业务名称',
            field: 'comboName'
        }, {
            field: 'accNbr',
            desc: '产品号'
        }, {
            field: 'areaName',
            desc: '片区'
        }, {
            field: 'relaPhone',
            desc: '联系电话'
        }, {
            field: 'mobilePhone',
            desc: '移动电话'
        }, {
            field: 'relaAddr',
            desc: '联系地址'
        }, {
            field: 'orderState',
            desc: '受理状态',
            type: 'int',
            renderer: function(val){
	            var str;
	            switch (val) {
	            	case -1:
	            		str = '处理中';
	                	break;
	            	case 0:
	            		str = '受理成功';
	                	break;
	                case 1:
	                    str = '受理失败';
	                    break;
	                case 2:
	                    str = '业务撤单';
	                    break;
	                default:
	                    str = '';
	                    break;
	            }
	            return str;
	        }
        }, {
            field: 'state',
            desc: '工单状态',
            type: 'int',
            renderer: function(val){
	            var str;
	            switch (val) {
		            case -2:
	                    str = '该工单失效';
	                    break;
	                case -1:
	                    str = '生成工单失败';
	                    break;
	            	case 0:
	            		str = '未处理';
	                	break;
	                case 1:
	                    str = '已完工';
	                    break;
	                case 2:
	                    str = '处理中';
	                    break;
	                default:
	                    str = '';
	                    break;
	            }
	            return str;
	        }
        }, {
            field: 'createStaff',
            desc: '创建人',
            type: 'int',
            hidden: true
        }, {
            field: 'modifyStaff',
            desc: '修改人',
            type: 'time',
            hidden: true
        }, {
            field: 'createTime',
            desc: '创建时间',
            type: 'time',
            hidden: true
        }, {
            field: 'modifyTime',
            desc: '修改时间',
            type: 'time',
            hidden: true
        }, {
            field: 'dealDesc',
            desc: '处理描述'
        }];

         // 定义表单
        var orderForm = new CQCIS.form.DWRForm({
            dwr: {
                load: dwr.getOrderQueue,
                save: dwr.createOrderQueue,
                update: dwr.updateOrderQueue
            },
            metas: orderFormMeta,
            listeners: {
                'saveafter': function(){
                    CQCIS.msg('工单信息保存成功！');
                    grid.reload();
                }
            }
        });
        
        // 定义窗口
        var orderWin = new CQCIS.form.WinForm({
            title: '工单信息',
            width: 450,
            height: 420,
            items: orderForm
        });
        
        //工单查询
        var searchBtn = new CQCIS.Button({
			text :'查询(<u>Q</u>)',
			iconCls :'btn_search',
			handler : function() {
				if (Ext.getBody().dom.className.indexOf('x-body-masked') < 0) {
					if(!TypeAcbx.getValue()){
						CQCIS.msg('请输入查询方式!');
                        return;
					}
				
					delete grid.filter.orderId;
					delete grid.filter.accNbr;
					delete grid.filter.createTime;
					delete grid.filter.modifyTime;
					delete grid.filter.orderState;
					delete grid.filter.state;
					delete grid.filter.areaId;
					if (TypeAcbx.getValue() === '0') {
						grid.reload();
					}
					if (TypeAcbx.getValue() === '1') {
					//按工单号
						if (!SearchBox.getValue()){
							 CQCIS.msg('请输入查询关键字!');
                             return;
						}
                        grid.filter.orderId = SearchBox.getValue();
                        grid.reload();
					}
					if (TypeAcbx.getValue() === '2') {
					//按帐号
						if (!SearchBox.getValue()){
							 CQCIS.msg('请输入查询关键字!');
                             return;
						}
						grid.filter.accNbr = SearchBox.getValue();
						grid.reload();
					}
					if (TypeAcbx.getValue() === '3') {
					//按受理时间
						if (!StartDate.getValue()) {
							CQCIS.msg('请选择查询开始时间!');
                             return;
                        }
						if (!EndDate.getValue()) {
							CQCIS.msg('请选择查询结束时间!');
	                        return;
						}

						grid.filter.createTime = new Date(StartDate.getValue()).getTime();
						grid.filter.createTime = new Date(EndDate.getValue()).getTime();
						grid.reload();
					}
					if (TypeAcbx.getValue() === '4') {
					//按工单状态
						if (!stateAcbx.getValue()) {
							 CQCIS.msg('请选择工单状态!');
                             return;
						}
						grid.filter.state = stateAcbx.getValue();
						grid.reload();
					}
					if (TypeAcbx.getValue() === '5') {
					//按受理状态
						if (!orderStateAcbx.getValue()) {
							 CQCIS.msg('请选择工单状态!');
                             return;
						}
						grid.filter.orderState = orderStateAcbx.getValue();
						grid.reload();
					}
					if (TypeAcbx.getValue() === '6') {
					//按片区
						if (!areaCbx.getValue()) {
							 CQCIS.msg('请选择工单状态!');
                             return;
						}
						grid.filter.areaId = areaCbx.getValue();
						grid.reload();
					}
				}
			}
		});
		
		// 查看工单详细信息
        var orderDetailWin = new CQCIS.form.WinForm({
            title: '工单详细信息',
            width: 450,
            height: 380,
            autoScroll: true,
            html: '',
            buttons: [{
                text: '关闭',
                handler: function(){
                    orderDetailWin.hide();
                }
            }],
            listeners: {
            	'beforeshow': function(){
            		if(!grid.getSelectedRow()){
            			CQCIS.msg('请选择您要查看的工单！');
            			return false;
            		}
            	},
                'show': function(o){
                    if (o.rendered) {
                    	var filter = {
                    		orderId: grid.getSelectedRow().orderId
                    	};
                    	var like = {};
                    	var sort = {};
						dwr.pageOrderQueue(filter, like, sort, 1, 1, {
							callback: function(lv){
								var htmlStr = [];
								var obj = lv.result[0];
								
								var orderState;
								switch (obj.orderState) {
									case -1:
					            		orderState = '处理中';
					                	break;
					            	case 0:
					            		orderState = '受理成功';
					                	break;
					                case 1:
					                    orderState = '受理失败';
					                    break;
					                case 2:
					                    orderState = '业务撤单';
					                    break;
					                default:
					                    orderState = '';
					                    break;
					            }
					        	var state;
					        	switch (obj.state) {
						            case -2:
					                    state = '该工单失效';
					                    break;
					                case -1:
					                    state = '生成工单失败';
					                    break;
					            	case 0:
					            		state = '未处理';
					                	break;
					                case 1:
					                    state = '已完工';
					                    break;
					                case 2:
					                    state = '处理中';
					                    break;
					                default:
					                    state = '';
					                    break;
					            }
								htmlStr.push('<table border="1" cellpadding="0" cellspacing="1" width="100%" >');
								htmlStr.push('<tr><td width="100">工单号:</td><td>',obj.orderId,'</td></tr>');
								htmlStr.push('<tr><td width="100">CRM工单号:</td><td>',obj.crmSeq,'</td></tr>');
								htmlStr.push('<tr><td width="100">客户名:</td><td>',obj.custName,'</td></tr>');
								htmlStr.push('<tr><td width="100">证件号码:</td><td>',obj.cardCode,'</td></tr>');
								htmlStr.push('<tr><td width="100">产品类型:</td><td>',obj.prodTypeDesc,'</td></tr>');
								htmlStr.push('<tr><td width="100">套餐名称:</td><td>',obj.comboName,'</td></tr>');
								htmlStr.push('<tr><td width="100">产品号码:</td><td>',obj.accNbr,'</td></tr>');
								htmlStr.push('<tr><td width="100">所属片区:</td><td>',obj.areaName,'</td></tr>');
								htmlStr.push('<tr><td width="100">联系电话:</td><td>',obj.relaPhone,'</td></tr>');
								htmlStr.push('<tr><td width="100">移动号码:</td><td>',obj.mobilePhone,'</td></tr>');
								htmlStr.push('<tr><td width="100">联系地址:</td><td>',obj.relaAddr,'</td></tr>');
								htmlStr.push('<tr><td width="100">工单状态:</td><td>',orderState,'</td></tr>');
								htmlStr.push('<tr><td width="100">处理状态:</td><td>',state,'</td></tr>');
								//htmlStr.push('<tr><td width="100">录入工号:</td><td>',obj.createStaff,'</td></tr>');
								//htmlStr.push('<tr><td width="100">修改工号:</td><td>',obj.modifyStaff,'</td></tr>');
								htmlStr.push('<tr><td width="100">创建时间:</td><td>',obj.createTime.format('Y-m-d H:i:s'),'</td></tr>');
								htmlStr.push('<tr><td width="100">修改时间:</td><td>',obj.modifyTime.format('Y-m-d H:i:s'),'</td></tr>');
								htmlStr.push('<tr><td width="100">处理原因:</td><td>',obj.dealDesc,'</td></tr>');
								htmlStr.push('</table>');
								var str = {
									inner: htmlStr.join('')
								};
								o.body.dom.innerHTML = htmlStr.join('');
							}
						});
                    } else {
                        o.show.defer(100, o);
                    }
                }
            }
        });
        
        var detailBtn = new CQCIS.Button({
            text: '详情 (<u>L</u>)',
            iconCls: 'btn_insert',
			handler: function(){ 
			 	orderDetailWin.load(); 
            }
        });
        
        // 片区信息grid
        var grid = new CQCIS.grid.PageGrid({
            firstColumn: 'radio',
            dwr: {
                call: dwr.pageOrderQueue
            },
            filter: {},
            sortable: true,
            metas: orderPageMeta,
            tbar: ['-', TypeText, TypeAcbx, '-', 
            		OrderText, NbrText, SearchBox, 
            		TimeText, StartDate, UtilText, EndDate, 
            		stateText, stateAcbx,
            		orderStateText, orderStateAcbx,
            		areaText, areaCbx,
            		'-', searchBtn, '-', addBtn, '-', editBtn, '-', detailBtn
            	],
            bbar: true,
            initload: true,
            listeners: {
            	'render' : function() {
            			OrderText.show();
						NbrText.hide();
						SearchBox.show();
	            		TimeText.hide();
	            		StartDate.hide();
	            		UtilText.hide();
	            		EndDate.hide();
	            		stateText.hide();
	            		stateAcbx.hide();
	            		orderStateText.hide();
	            		orderStateAcbx.hide();
	            		areaText.hide();
	            		areaCbx.hide();
	            		if(top.CURRENTUSER.loginlevel > 2){
	            			editBtn.hide();
	            		}
				},
                'rowdblclick': editBtn.handler,
                'rowcontextmenu': function(o, rowIndex, e){
                    e.stopEvent();
                    //定义增加右键菜单
                    var addMenu = new Ext.menu.Item({
                        iconCls: 'menu_insert',
                        id: 'addMenu',
                        text: '增加',
                        handler: addBtn.handler
                    });
                    //定义修改右键菜单
                    var editMenu = new Ext.menu.Item({
                        iconCls: 'menu_modify',
                        id: 'editMenu',
                        text: '修改',
                        handler: editBtn.handler
                    });
                    //定义列表右键菜单
                    var gridMenu = new Ext.menu.Menu({
                        id: 'gridMenu',
                        items: [addMenu]
                    });
                    //gridMenu.showAt(e.getXY());
                }
            }
        });
        grid.render();
    }
};

Ext.onReady(area.init, area);
